package com.umer.pinban.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.common.base.activity.BaseActivity;
import com.common.bean.InfoResult;
import com.common.bean.MsgBean;
import com.common.bean.UserInfo;
import com.common.okhttp.OkEntityListRequest;
import com.common.okhttp.OkEntityRequest;
import com.common.okhttp.OkSimpleRequest;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.ui.recycleview.listener.OptListener;
import com.common.utils.AntiShake;
import com.common.utils.Constants;
import com.common.utils.EventConstants;
import com.common.utils.GlideUtils;
import com.lxj.xpopup.XPopup;
import com.makeramen.roundedimageview.RoundedImageView;
import com.umer.pinban.AppHolder;
import com.umer.pinban.R;
import com.umer.pinban.adapter.user.PayModeAdapter;
import com.umer.pinban.adapter.user.SelectMemberListAdapter;
import com.umer.pinban.bean.payMode;
import com.umer.pinban.bean.user.SelectMemberListBean;
import com.umer.pinban.bean.user.SubmitBuyMemberBean;
import com.umer.pinban.user.dialog.DialogVip;
import com.umer.pinban.user.dialog.DialogXieYi;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

public class XuFeiActivity extends BaseActivity implements OptListener {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.my_image_view)
    RoundedImageView myImageView;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.tv_shop_time)
    TextView tvShopTime;
    @BindView(R.id.recycler1)
    RecyclerView recycler1;
    @BindView(R.id.recycler2)
    RecyclerView recycler2;
    @BindView(R.id.tv_1)
    TextView tv1;
    @BindView(R.id.linear_1)
    LinearLayout linear1;
    @BindView(R.id.tv_2)
    TextView tv2;
    @BindView(R.id.linear_2)
    LinearLayout linear2;
    @BindView(R.id.tv_3)
    TextView tv3;
    @BindView(R.id.linear_3)
    LinearLayout linear3;
    private UserInfo userInfo;
    private SelectMemberListAdapter selectMemberListAdapter;
    private List<SelectMemberListBean> selectMemberListBeanList = new ArrayList<>();
    private LinearLayoutManager linearLayoutManager;

    private LinearLayoutManager linearLayoutManager2;
    private PayModeAdapter payModeAdapter;
    private List<payMode> payModeList = new ArrayList<>();
    private String payMethod = "";

    //需要传递的
    private String remark= "";
    private String price = "";
    private String content = "";

    private DialogXieYi dialogXieYi;
    private DialogVip dialogVip;

    @Override
    public View isSupportLoadSirEnabled() {
        return null;
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                //是否可以修改导航栏颜色，默认为true
                .navigationBarEnable(true)
                //导航栏颜色，不写默认黑色
                .navigationBarColor(R.color.barcolor)
                .statusBarColor(R.color.colorPrimary)
                //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_xufei;
    }

    @Override
    public void init() {
        userInfo = AppHolder.getInstance().getUserInfo();
        dialogXieYi = new DialogXieYi(this);
        dialogXieYi.setEventBus(getEventBus());
        dialogVip = new DialogVip(this);
        dialogVip.setEventBus(getEventBus());

        if (userInfo != null) {
            GlideUtils.getInstance().loadHeadImageView(this,
                    Constants.IP_PORT_DEFAULT_PICTURE + userInfo.getHeader(), myImageView);
            if (userInfo.getName() != null && !userInfo.getName().equals("null")
                    && !userInfo.getName().equals(" ") && !userInfo.getName().equals("")) {
                tvShopName.setText(userInfo.getName());
            } else {
                tvShopName.setText("");
            }
            //到期
            if (userInfo.getMemberTime() != null && !userInfo.getMemberTime().equals("null")
                    && !userInfo.getMemberTime().equals(" ") && !userInfo.getMemberTime().equals("")) {
                tvShopTime.setText(userInfo.getMemberTime()+"到期");
            } else {
                tvShopTime.setText("续费");
            }
        }

        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        selectMemberListAdapter = new SelectMemberListAdapter(this,selectMemberListBeanList,
                R.layout.activity_xufei_item,this);
        recycler1.setLayoutManager(linearLayoutManager);
        recycler1.setAdapter(selectMemberListAdapter);

        linearLayoutManager2 = new LinearLayoutManager(this);
        linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        payModeAdapter = new PayModeAdapter(this,payModeList,
                R.layout.activity_zhifu_item,this);
        recycler2.setLayoutManager(linearLayoutManager2);
        recycler2.setAdapter(payModeAdapter);
    }

    @Override
    public void loadData() {
        OkEntityListRequest<SelectMemberListBean> request = new OkEntityListRequest<>(R.id.selectMemberList,
                Constants.selectMemberList, SelectMemberListBean.class);
        request.addParams("shopId", userInfo.getShopId());
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    private void getPayMethod(){
        OkEntityListRequest<payMode> request = new OkEntityListRequest<>(R.id.payMode,
                Constants.payMode, payMode.class);
        request.addParams("type", "buyMember");
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    @OnClick({R.id.linear_back,R.id.linear_2,R.id.linear_3,R.id.linear_1})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                finish();
                break;
            //会员兑换
            case R.id.linear_1:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                showVip();
                break;
            case R.id.linear_2:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                switchTo(XuFeiActivity.this,XuFeiRecordListActivity.class,false);
                break;
            //协议
            case R.id.linear_3:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                showXieYi();
                break;
            default:
                break;
        }
    }

    @Override
    public void onOptClick(View view, Object object) {
        int index = (int) object;
        switch (view.getId()){
            case R.id.linear_pay:
                payModeList.get(index).setSelect(true);
                payMethod = payModeList.get(index).getValue();
                for(int i=0;i<payModeList.size();i++){
                    if(i!=index){
                        payModeList.get(i).setSelect(false);
                    }
                }
                payModeAdapter.notifyDataSetChanged();
                break;
            //续费
            case R.id.linear_lay:
                int shopMemberId = selectMemberListBeanList.get(index).getShopMemberId();
                remark = selectMemberListBeanList.get(index).getRemark();
                price =  selectMemberListBeanList.get(index).getPrice();
                content =  selectMemberListBeanList.get(index).getContent();
                showProgress("正在处理,请稍后...");
                OkEntityRequest<SubmitBuyMemberBean> request = new OkEntityRequest<>(R.id.submitBuyMember,
                        Constants.submitBuyMember, SubmitBuyMemberBean.class);
                request.addParams("shopId", userInfo.getShopId());
                request.addParams("shopMemberId", shopMemberId);
                request.addParams("paymentMode", payMethod);
                request.setHeader(true);
                requestOkhttpEntity(request);
                break;
            default:break;
        }
    }

    @Override
    public void handlerMeg(MsgBean msgBean) {
        super.handlerMeg(msgBean);
        int flag = msgBean.getFlag();
        if(flag == EventConstants.input_vip_code){
            String code = (String) msgBean.getObject();
            showProgress("正在处理,请稍后...");
            OkSimpleRequest okSimpleRequest = new OkSimpleRequest(R.id.exchangeMember,
                    Constants.exchangeMember);
            okSimpleRequest.addParams("shopId", userInfo.getShopId());
            okSimpleRequest.addParams("code", code);
            okSimpleRequest.setHeader(true);
            requestOkhttpSimple(okSimpleRequest);
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.exchangeMember:
                showToast(t.getDesc());
                finish();
                break;
            case R.id.selectMemberList:
                selectMemberListBeanList = (List<SelectMemberListBean>) t.getT();
                if(selectMemberListBeanList!=null && selectMemberListBeanList.size()>0){
                    selectMemberListAdapter.setDataSource(selectMemberListBeanList);
                    getPayMethod();
                }
                break;
            case R.id.payMode:
                payModeList = (List<payMode>) t.getT();
                if(payModeList!=null && payModeList.size()>0){
                    payModeList.get(0).setSelect(true);
                    payMethod = payModeList.get(0).getValue();
                    payModeAdapter.setDataSource(payModeList);
                }
                break;
            case R.id.submitBuyMember:
                hideProgress();
                SubmitBuyMemberBean submitBuyMemberBean = (SubmitBuyMemberBean) t.getT();
                if(submitBuyMemberBean!=null){
                    String paymentNo = submitBuyMemberBean.getPaymentNo();
                    String qrPath = submitBuyMemberBean.getQrPath();
                    Intent intent = new Intent(XuFeiActivity.this,SweepCodePayActivity.class);
                    intent.putExtra("paymentNo",paymentNo);
                    intent.putExtra("qrPath",qrPath);
                    intent.putExtra("content",content);
                    intent.putExtra("price",price);
                    intent.putExtra("remark",remark);
                    startActivity(intent);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.selectMemberList:
                break;
            case R.id.payMode:
                break;
            case R.id.submitBuyMember:
                hideProgress();
                showToast(t.getDesc());
                break;
            case R.id.exchangeMember:
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }

    private void showXieYi() {
        dialogXieYi = new DialogXieYi(this);
        new XPopup.Builder(this)
                .asCustom(dialogXieYi)
                .show();
    }

    private void showVip() {
        dialogVip = new DialogVip(this);
        new XPopup.Builder(this)
                .asCustom(dialogVip)
                .show();
    }
}
